#!/bin/bash
# ----------------------------------------------------------------
#   5-RunFEAT.sh: Run the standard fMRI analysis pipeline
#
#	Usage:
#		5-RunFEAT.sh rawDataSrc projectDir subjID
#   initializes the Adaptation fMRI experiment. This experiment 
#   effect of stimulus history on the current evoked response,
#   on serial position -1 to -N are manipulated in a 
#
#
#   Inputs
#   -------
#   [protocolListFileName]: path to a file that should contain a
#   	description of the protocols to be used. If nothing is
#   	the file ./Config/ProtocolList.cfg will be used
#
#
#   Outputs
#   -------
#   ./Data/[expType]/[protocol]/[subjID]/[subjID]-[protocol]-
#  	this file will store all experimental info, experiment
#   	parameters, stimuli used, and data collected
#
#   Examples
#   -------
#    i) AdaptID_v1_1('./protocols.txt')
#   ii) AdaptID_v1_1
#
# ----------------------------------------------------------------
# ``Redistribution and use, with or without modification, are
#	permitted provided that the list of contributors below is
#	preserved.``
#
#   Original code by Marcelo G Mattar (09/04/2012)
#	mattar@sas.upenn.edu
# ----------------------------------------------------------------


# --- Check input command ---
if [ $# -ne 4 ]; then
cat << EOF
USAGE: `basename $0` dataDir analysisDir subjectID scanName
EOF
	exit 1
fi

# --- Read input parameters ---
dataDir=$1
analysisDir=$2
subjectID=$3
scanName=$4

# --- Set up some important variables ---
OCD=$PWD
BASEDIR=$(dirname $0)
TEMPLATEFILE=$BASEDIR/FEAT_2ndLevel.fsf



echo Working on 2nd level analysis - $scanName
		
# --- Define the name of the temporary design file ---
DESIGNFILE=`echo $BASEDIR/FEAT_${subjectID}_${scanName}_2ndLevel.fsf`
		
# --- Create a temporary design file ---
cp $TEMPLATEFILE $DESIGNFILE
		
		
# -----------------------------------------------------
# 1) --- BASIC PARAMETERS ---
# -----------------------------------------------------

FEATDIR_1=$analysisDir/$subjectID/${scanName}1.feat #4D AVW data or FEAT directory (/jet/mattar/AdaptID/R091412V/Face1.feat)
FEATDIR_2=$analysisDir/$subjectID/${scanName}2.feat #4D AVW data or FEAT directory (/jet/mattar/AdaptID/R091412V/Face2.feat)
FEATDIR_3=$analysisDir/$subjectID/${scanName}3.feat #4D AVW data or FEAT directory (/jet/mattar/AdaptID/R091412V/Face3.feat)
FEATDIR_4=$analysisDir/$subjectID/${scanName}4.feat #4D AVW data or FEAT directory (/jet/mattar/AdaptID/R091412V/Face4.feat)

OUTPUTDIR=$analysisDir/$subjectID/${scanName}.gfeat #Output directory (/jet/mattar/AdaptID/Analyses/R091412V/Face.gfeat)
NUMSCANS=4 #Total volumes (4)


# -----------------------------------------------------
# 2) --- STATS PARAMETERS ---
# -----------------------------------------------------

FIXED_VS_MIXED_EFFECTS=3 #Higher-level modelling (0-3, see template file)
NUM_CONTRASTS=3 #Number of lower-level copes feeding into higher-level analysis (3)
		

# -----------------------------------------------------
# 3) --- POSTSTATS PARAMETERS ---
# -----------------------------------------------------

THRESHOLDING=3 #Thresholding (0-3, see template file)
P_THRESHOLD=0.05 #P threshold (0.05)
Z_THRESHOLD=2.3 #Z threshold (2.3)
BACKGROUND_IMAGE=5 #Background image for higher-level stats overlays (1-5, see template file)
REGFILE_3=$dataDir/$subjectID/Anatomy/BET/MPRAGE_brain_0.4 #Standard image (/jet/mattar/AdaptID/Data/R091412V/Anatomy/BET/MPRAGE_brain_0.4)
		
		
# -----------------------------------------------------
# 4) --- PERFORM SUBSTITUTIONS ON DESIGN FILE ---
# -----------------------------------------------------
		
sed -i 's|##FEATDIR_1##|'$FEATDIR_1'|' $DESIGNFILE
sed -i 's|##FEATDIR_2##|'$FEATDIR_2'|' $DESIGNFILE
sed -i 's|##FEATDIR_3##|'$FEATDIR_3'|' $DESIGNFILE
sed -i 's|##FEATDIR_4##|'$FEATDIR_4'|' $DESIGNFILE
sed -i 's|##OUTPUTDIR##|'$OUTPUTDIR'|' $DESIGNFILE
sed -i 's|##NUMSCANS##|'$NUMSCANS'|' $DESIGNFILE

sed -i 's|##FIXED_VS_MIXED_EFFECTS##|'$FIXED_VS_MIXED_EFFECTS'|' $DESIGNFILE
sed -i 's|##NUM_CONTRASTS##|'$NUM_CONTRASTS'|' $DESIGNFILE

sed -i 's|##THRESHOLDING##|'$THRESHOLDING'|' $DESIGNFILE
sed -i 's|##P_THRESHOLD##|'$P_THRESHOLD'|' $DESIGNFILE
sed -i 's|##Z_THRESHOLD##|'$Z_THRESHOLD'|' $DESIGNFILE
sed -i 's|##BACKGROUND_IMAGE##|'$BACKGROUND_IMAGE'|' $DESIGNFILE
sed -i 's|##REGFILE_3##|'$REGFILE_3'|' $DESIGNFILE

		
# -----------------------------------------------------
# 5) --- RUN FEAT ---
# -----------------------------------------------------

feat $DESIGNFILE

# -----------------------------------------------------
# 8) --- REMOVE TEMPORARY DESIGN FILE ---
# -----------------------------------------------------

rm -f $DESIGNFILE
		

cd $OCD

exit 0